package com.freesky.web.manager.exception;

import com.freesky.common.exception.AjaxAuthorizationException;
import com.freesky.common.exception.MyException;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常捕获
 * @author Administrator
 *
 */
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {
	//权限不足,非ajax
	@ExceptionHandler(value=AuthorizationException.class)
	public String authorizationException(){
		return "403";
	}
	//权限不足,ajax
	@ResponseBody
	@ExceptionHandler(value= AjaxAuthorizationException.class)
	public JSONResponse ajaxAuthorizationException(){
		return JSONResponseDiretor.buildLackPermJSONResponse(null);
	}

	/**
	 * 统一异常捕获
	 * @param e
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler(value= MyException.class)
	public JSONResponse allException(MyException e){
		if (e.getThrowable() != null) {
			log.error("统一异常信息打印{}",e.getThrowable());
		}
		return JSONResponseDiretor.buildMyExceptionJSONResponse(e);
	}
}
